#include "functions.h"
#include <random>

int main()
{
  for(int n = 5; n < 31; n++)
    {
      double** A = new double*[n];
      double** B = new double*[n];
      for(int i = 0; i < n; i++)
	{
	  A[i] = new double[n]();
	  B[i] = new double[n]();
	  A[i][i] = A[i][n-1] = 1.0;
	  B[i][i] = B[i][n-1] = 1.0;
	  for(int j = 0; j < i ; j++)
	    {
	      A[i][j] = -1.0;
	      B[i][j] = -1.0;
	    }
	}

      //随机生成x,x为精确解
      double* x = new double[n]();
      for(int i = 0; i < n; i++)
	{
	  std::random_device e;
	  x[i] = 1.0/((double)(e()%1000));
	}

      //根据随机生成的x计算b=Ax
      double* b1 = new double[n]();
      Matrix_Vector_Product(B, x, b1, n);
      double* b2 = new double[n]();
      Matrix_Vector_Product(B, x, b2, n);
      int* p = new int[n]();

      //对Ax=b进行列主元高斯消去法求解，得到计算解储存在b1中，为计算解
      Column_Principle_Gaussian_Elimination(A, p, n);
      for(int i = 0; i < n; i++)
	{
	  if(p[i] > i)
	    {
	      double tmp = b1[i];
	      b1[i] = b1[p[i]];
	      b1[p[i]] = b1[i];
	    }
	}
      delete [] p;
      Unit_Lower_Triangular_Matrix(A, b1, n);
      Upper_Triangular_Matrix(A, b1, n);

      //求计算解b1和精确解x的相对误差估计和真实相对误差
      Precision(B, b2, b1, x, n);
      
      for(int i = 0; i < n; i++)
	{
	  delete [] A[i];
	  delete [] B[i];
	}
      delete [] A;
      delete [] B;
      delete [] x;
      delete [] b1;
      delete [] b2;
    }
};
